package com.daka.controller;

import com.daka.enumerate.StatusCode;
import com.daka.service.FileUtil;
import com.daka.tools.QNiuToken;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/index")
@Slf4j
public class IndexController {

    @Resource
    private FileUtil fileUtil;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/uploadFile", headers = "content-type=multipart/*")
    public BaseResponseVo upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        String dir = request.getSession().getServletContext().getRealPath("/upload");// + Constants.UPLOADDIR;
        fileUtil.uploadFile(file, dir);

        return new BaseResponseVo(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getConfig")
    @ResponseBody
    public DataResponseVo<HashMap<String, Object>> token() {

        String token = QNiuToken.token("dakaqi");

        HashMap<String, Object> hash = new HashMap<String, Object>();
        hash.put("qiniuToken",token);

        return new DataResponseVo<HashMap<String, Object>>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), hash);
    }
}
